package cn.yunhe.service;

import cn.yunhe.dao.KeySearchDao;
import cn.yunhe.entity.Library;
import cn.yunhe.util.SqlSessionFactoryUtil;
import net.sf.json.JSONArray;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class KeySearchService {
    SqlSession session=null;
    KeySearchDao keySearchDao=null;
    public JSONArray keySearchByName(String keyword){
        JSONArray jsonArray=new JSONArray();
        try{
            session= SqlSessionFactoryUtil.getSqlSession();
            keySearchDao=session.getMapper(KeySearchDao.class);
            List<Library> libraryList=keySearchDao.keySearchByName(keyword);
            jsonArray.addAll(libraryList);
        }finally {
            session.close();
        }

        return jsonArray;
    }

    public JSONArray keySearchByAuthor(String keyword){
        JSONArray jsonArray=new JSONArray();
        try{
            session= SqlSessionFactoryUtil.getSqlSession();
            keySearchDao=session.getMapper(KeySearchDao.class);
            List<Library> libraryList=keySearchDao.keySearchByAuthor(keyword);
            jsonArray.addAll(libraryList);
        }finally {
            session.close();
        }

        return jsonArray;
    }

    public JSONArray keySearchByPublisher(String keyword){
        JSONArray jsonArray=new JSONArray();
        try{
            session= SqlSessionFactoryUtil.getSqlSession();
            keySearchDao=session.getMapper(KeySearchDao.class);
            List<Library> libraryList=keySearchDao.keySearchByPublisher(keyword);
            jsonArray.addAll(libraryList);
        }finally {
            session.close();
        }

        return jsonArray;
    }
}
